package org.bagou.xiangs.framework_mvp.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import org.bagou.xiangs.framework_mvp.R
import org.bagou.xiangs.framework_mvp.model.WechatItem

class MainMvpAdapter : RecyclerView.Adapter<MainMvpAdapter.MainMvpViewHolder>() {
    private val list:ArrayList<WechatItem> = ArrayList()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainMvpViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val rootView = inflater.inflate(R.layout.item_mvp_user, parent, false)
        return MainMvpViewHolder(rootView)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: MainMvpViewHolder, position: Int) {
        onViewLayoutSet(holder, position)
        holder.textView!!.text = list[position].itemName
        Glide.with(holder.imgView!!).load(list[position].itemIcon).into(holder.imgView!!)
    }

    fun addData(list: List<WechatItem>) {
        this.list.addAll(list)
    }


    class MainMvpViewHolder(public val rootView: View) : RecyclerView.ViewHolder(rootView) {
        var textView: TextView? = null
        var imgView: ImageView? = null
        init {
            this.textView = this.rootView.findViewById<TextView>(R.id.item_text)
            this.imgView = this.rootView.findViewById(R.id.item_icon)
        }
    }


    // 设置控件样式
    private fun onViewLayoutSet(holder: MainMvpViewHolder, position: Int) {
        val layoutParams: ViewGroup.LayoutParams = holder.rootView!!.layoutParams
        when (position) {
            0 -> {
                (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 30
                (layoutParams as? ViewGroup.MarginLayoutParams)?.bottomMargin = 30
            }
            itemCount - 1 -> (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 30
            else -> (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 1
        }
        holder.rootView!!.layoutParams = layoutParams
    }
}
